import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import os

class Register:
    def __init__(self):
        # 打开浏览器
        self.driver = webdriver.Chrome()
        # 获取本地路径并请求网址
        self.url = 'file:///' + os.path.abspath('html') + '/注册实例.html'
        self.driver.get(self.url)

    def frame1(self):
        time.sleep(2)
        # 获取外层frame账号在账号输入框输入内容
        self.driver.find_element(By.ID, 'user').send_keys('whats')
        time.sleep(2)
        # 获取密码输入框并输入内容
        self.driver.find_element(By.ID, 'password').send_keys('123456')
        time.sleep(2)
        # 获取电话号码输入框并输入内容
        self.driver.find_element(By.ID, 'tel').send_keys('15071269444')
        time.sleep(2)
        # 获取电子邮件输入框并输入内容
        self.driver.find_element(By.ID, 'email').send_keys('what@163.com')
        time.sleep(2)
        # 获取注册用户按钮并点击
        self.driver.find_element(By.XPATH, '//*[@id="zc"]/fieldset/button').click()

    def frame2(self):
        time.sleep(2)
        # 切换到注册用户A
        self.driver.switch_to.frame('idframe1')
        register_A = self.driver.find_element(By.ID, 'zc')
        time.sleep(2)
        # 获取注册用户A的账号输入框并输入内容
        self.driver.find_element(By.ID, 'userA').send_keys('Where')
        time.sleep(2)
        # 获取密码输入框并输入内容
        self.driver.find_element(By.ID, 'passwordA').send_keys('123456')
        time.sleep(2)
        # 获取电话号码输入框并输入内容
        self.driver.find_element(By.ID, 'telA').send_keys('15071269443')
        time.sleep(2)
        # 获取电子邮件输入框并输入内容
        self.driver.find_element(By.ID, 'emailA').send_keys('where@163.com')
        time.sleep(2)
        # 获取注册用户按钮并点击
        self.driver.find_element(By.XPATH, '//*[@id="zc"]/fieldset/button').click()

    def frame3(self):
        time.sleep(2)
        # 切换到注册用户A
        self.driver.switch_to.frame('myframe2')
        register_A = self.driver.find_element(By.ID, 'zc')
        time.sleep(2)
        # 获取注册用户A的账号输入框并输入内容
        self.driver.find_element(By.ID, 'userB').send_keys('When')
        time.sleep(2)
        # 获取密码输入框并输入内容
        self.driver.find_element(By.ID, 'passwordB').send_keys('123456')
        time.sleep(2)
        # 获取电话号码输入框并输入内容
        self.driver.find_element(By.ID, 'telB').send_keys('15071269442')
        time.sleep(2)
        # 获取电子邮件输入框并输入内容
        self.driver.find_element(By.ID, 'emailB').send_keys('when@163.com')
        time.sleep(2)
        # 获取注册用户按钮并点击
        self.driver.find_element(By.XPATH, '//*[@id="zc"]/fieldset/button').click()

    def quit(self):
        time.sleep(2)
        self.driver.quit()

if __name__ == '__main__':
    register = Register()
    register.frame1()
    register.frame2()
    register.frame3()
    register.quit()